package com.kun.service;

import com.kun.common.Constants;
import com.kun.common.Result;
import com.kun.entity.RoleMenu;
import com.kun.exception.MyServiceException;
import com.kun.mapper.RoleMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleMenuServiceImpl implements RoleMenuService {

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public Result addRoleMenu(RoleMenu roleMenu) {
        if (roleMenu.getRoleId()==null||roleMenu.getMenuId()==null){
            throw new MyServiceException(Constants.CODE_500,"角色id和菜单id不可为空");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("roleId",roleMenu.getRoleId());
        map.put("menuId",roleMenu.getMenuId());
        if(roleMenuMapper.selectAllRoleMenus(map).size()>0){
            throw new MyServiceException(Constants.CODE_500,"该记录在数据库中已经存在");
        }
        roleMenuMapper.addRoleMenu(roleMenu);
        return Result.success();
    }

    @Override
    @Transactional
    public Result deleteRoleMenu(Integer roleId) {
        roleMenuMapper.deleteRoleMenu(roleId);
        return Result.success();
    }



    @Override
    public List<RoleMenu> selectAllRoleMenus(Map map) {
        return roleMenuMapper.selectAllRoleMenus(map);
    }
}
